package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1:新建一个 shoppingCart对象 购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //先把 dto里面的信息拷贝到 购物车对象中
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //  菜品  dishFlavor dishId  套餐 setmealId
        // 问题1 怎么获取用户id
        Long userId = BaseContext.getCurrentId();//因为在拦截器哪里 存进去了
        shoppingCart.setUserId(userId);
        //问题2  你怎么知道 该菜品/套餐 是 否存在在购物车中呢？
        //  菜品 根据 菜品id 口味  userId
        //  套餐 根据 套餐id  userId
        //判断当前商品 是否存在 于购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //问题3  有怎么做 没有怎么做
        if(shoppingCartList!=null && shoppingCartList.size()==1){
            //已经存在 就更新数量
            //其实查到的关联商品 只有一条数据  集合怎么取一条数据
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //不存在 就执行新增操作 插入数据
             // name  image  amount 在哪呢  在菜品表  在套餐表
            Long dishId = shoppingCart.getDishId();

            if(dishId!=null){
                //是菜品 数据从菜品表来
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //是套餐 数据从套餐表来
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //最终想做的 就是把这个购物项 添加到购物表中
            shoppingCartMapper.insert(shoppingCart);
        }



    }

    @Override
    public List<ShoppingCart> list() {
        // 根据用户id查询
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if(list != null && list.size() > 0){
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }

    /**
     * 清空购物车商品
     */
    public void cleanShoppingCart() {
        System.out.println("登录用户id："+BaseContext.getCurrentId());
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
